<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Translate.php';

/**
 * Helper to generate a translation of a text string
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3translate extends Zend_View_Helper_Abstract
{

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	 */
	private $_pi;

	/**
	 * Generates a translation of a text string
	 *
	 * @param string $str The text to translate
	 * @return string The translated text
	 */
	public function t3translate($str)
	{
		// Get translation from Zend_Translate if object is registered
		if(Zend_Registry::isRegistered('Zend_Translate')) {
			$translate = Zend_Registry::get('Zend_Translate');
			if($translate instanceof Zend_Translate) {
				return $translate->translate($str);
			}
		}
		$LL = $this->_pi->pi_getLL($str);
		return empty($LL) ? '(('.$str.'))' : $LL ;
	}

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
        return $this;
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3translate.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3translate.php']);
}
